package com.contest.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.contest.dao.RoomInfoDao;
import com.contest.entity.BettingRecord;
import com.contest.entity.RoomInfo;
import com.contest.entity.UserInfo;
import com.contest.service.BettingRecordService;
import com.contest.service.LimitSetupService;
import com.contest.service.RoomInfoService;
import com.contest.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author yfl
 * @since ${date}
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoDao, RoomInfo> implements RoomInfoService {

    @Autowired
    BettingRecordService bettingRecordService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    RoomInfoDao roomInfoDao;

    @Override
    @Transactional
    public boolean updateResult(RoomInfo roomInfo) {
        Integer result = roomInfo.getFdResult();
//        float todayAmount =
        if (result != 0) {
            float aTotalScale = 0.0f;
            float bTotalScale = 0.0f;
            float aSingleScale = 0.0f;
            float bSingleScale = 0.0f;
            String winId;
            float sizeScale = roomInfo.getFdSizeScale().floatValue();
            float specialScale = roomInfo.getFdSpecialScale().floatValue();
            int aKillNum = roomInfo.getFdAKillNum();
            int bKillNum = roomInfo.getFdBKillNum();
            String letId = roomInfo.getFdLetClanId();
            float letNum = roomInfo.getFdLetNum().floatValue();
            float aWinNum = roomInfo.getFdClanAWinNum().floatValue();
            float bWinNum = roomInfo.getFdClanBWinNum().floatValue();
            float totalNum = aWinNum + bWinNum;
            float winNum = totalNum / 2;
            String totalWinId;
            if (letId.equals(roomInfo.getFdClanAId())) {
                bWinNum += letNum;
            } else if (letId.equals(roomInfo.getFdClanBId())) {
                bWinNum += letNum;
            }
            if (aWinNum > winNum) {
                aTotalScale = roomInfo.getFdClanATotalScale().floatValue();
                totalWinId = roomInfo.getFdClanAId();
                computeTotal(totalWinId, aTotalScale, roomInfo.getFdId());
            } else if (bWinNum > winNum) {
                bTotalScale = roomInfo.getFdClanBTotalScale().floatValue();
                totalWinId = roomInfo.getFdClanBId();
                computeTotal(totalWinId, bTotalScale, roomInfo.getFdId());
            }
            if (result == -1) {
                //A胜
                aSingleScale = roomInfo.getFdClanAScale().floatValue();
                winId = roomInfo.getFdClanAId();
                computeSingle(roomInfo.getFdId(), winId, aSingleScale);
                roomInfo.setFdResult(-1);
            } else if (result == 1) {
                //B胜
                winId = roomInfo.getFdClanBId();
                bSingleScale = roomInfo.getFdClanBScale().floatValue();
                computeSingle(roomInfo.getFdId(), winId, bSingleScale);
                roomInfo.setFdResult(1);
            }
            computeSize((aKillNum + bKillNum) + "", roomInfo.getFdId(), sizeScale);
            computeSpecial((aKillNum + bKillNum) + "", specialScale, roomInfo.getFdId());
            roomInfo.setFdIsSettle(1);
            this.updateById(roomInfo);
            return true;
        } else {
            return this.updateById(roomInfo);
        }
    }

    @Override
    public List<RoomInfo> getList() {
        return roomInfoDao.getList();
    }

    /**
     * 计算总场
     * @param totalWinId
     * @param totalScale
     * @param roomId
     */
    private void computeTotal(String totalWinId, float totalScale, String roomId) {
        EntityWrapper<BettingRecord> recordEntityWrapper = new EntityWrapper<BettingRecord>();
        recordEntityWrapper.addFilter("fd_type={0} and fd_room_id={1} and fd_is_settlement={2}", 2, roomId, 0);
        List<BettingRecord> recordList = bettingRecordService.selectList(recordEntityWrapper);
        computeWin(recordList, totalWinId, totalScale);

    }

    /**
     * 计算特码
     *
     * @param totalKill
     * @param specialScale
     */
    private void computeSpecial(String totalKill, float specialScale, String roomId) {
        EntityWrapper<BettingRecord> recordEntityWrapper = new EntityWrapper<BettingRecord>();
        recordEntityWrapper.addFilter("fd_type={0} and fd_room_id={1} and fd_is_settlement={2}", 3, roomId, 0);
        List<BettingRecord> recordList = bettingRecordService.selectList(recordEntityWrapper);
        String lottery;
        if (totalKill.length() > 1) {
            lottery = totalKill.substring(totalKill.length() - 1, totalKill.length());
            computeResult(lottery, specialScale, recordList);
        } else {
            computeResult(totalKill, specialScale, recordList);
        }
    }

    /**
     * 计算单场胜负
     *
     * @param singleScale
     */
    private void computeSingle(String roomId, String winId, float singleScale) {
        EntityWrapper<BettingRecord> recordEntityWrapper = new EntityWrapper<BettingRecord>();
        recordEntityWrapper.addFilter("fd_type={0} and fd_room_id={1} and fd_is_settlement={2}", 0, roomId, 0);
        List<BettingRecord> recordList = bettingRecordService.selectList(recordEntityWrapper);
        computeWin(recordList, winId, singleScale);
    }

    private void computeWin(List<BettingRecord> recordList, String winId, float scale) {
        for (BettingRecord record : recordList) {
            String bettingClanId = record.getFdBettingWarId();
            float bettingAmount = record.getFdBettingAmount().floatValue();
            String userId = record.getFdUserId();
            //是否超额
            scale -= userInfoService.getLimitOdds(userId);
            if (bettingClanId.equals(winId)) {
                record.setFdResult(1);
                record.setFdWinLose(record.getFdBettingAmount());
                record.setFdIsSettlement(1);
                UserInfo userInfo = userInfoService.selectById(userId);
                float over = userInfo.getFdOver().floatValue();
                float winAmount = bettingAmount * scale;
                userInfo.setFdOver(BigDecimal.valueOf(over + winAmount + bettingAmount));
                userInfoService.updateById(userInfo);
            }
            record.setFdResult(-1);
            record.setFdWinLose(record.getFdBettingAmount());
            record.setFdIsSettlement(1);
            bettingRecordService.updateById(record);

        }
    }

    /**
     * 计算大小单双
     *
     * @param totalKillNum 击杀总人数
     * @param sizeScale    大小单双赔率
     * @return
     */
    private void computeSize(String totalKillNum, String roomId, float sizeScale) {
        EntityWrapper<BettingRecord> recordEntityWrapper = new EntityWrapper<BettingRecord>();
        recordEntityWrapper.addFilter("fd_type={0} and fd_room_id={1} and fd_is_settlement={2}", 1, roomId, 0);
        List<BettingRecord> recordList = bettingRecordService.selectList(recordEntityWrapper);
        int lastNum = Integer.parseInt(totalKillNum.substring(0,(totalKillNum.length() == 1 ? 2:totalKillNum.length()) - 1));
        //小
        if (lastNum <= 4) {
            String lottery = "01234";
            computeResult(lottery, sizeScale, recordList);
        }
        //大
        if (lastNum >= 5) {
            String lottery = "56789";
            computeResult(lottery, sizeScale, recordList);
        }
        //双
        if (lastNum == 0 || lastNum == 2 || lastNum == 4 || lastNum == 6 || lastNum == 8) {
            String lottery = "02468";
            computeResult(lottery, sizeScale, recordList);
        }
        //单
        if (lastNum == 1 || lastNum == 3 || lastNum == 5 || lastNum == 7 || lastNum == 9) {
            String lottery = "13579";
            computeResult(lottery, sizeScale, recordList);
        }
    }

    private void computeResult(String lottery, float scale, List<BettingRecord> recordList) {
        for (BettingRecord record : recordList) {
            String bettingNum = record.getFdBettingNum();
            String userId = record.getFdUserId();
            UserInfo userInfo = userInfoService.selectById(userId);
            //是否超额
            scale -= userInfoService.getLimitOdds(userId);
            float bettingAmount = record.getFdBettingAmount().floatValue();
            if (bettingNum.equals(lottery)) {
                record.setFdResult(1);
                record.setFdWinLose(record.getFdBettingAmount());
                record.setFdIsSettlement(1);
                bettingRecordService.updateById(record);
                if (userInfo != null) {
                    float over = userInfo.getFdOver().floatValue();
                    float winAmount = bettingAmount * scale;
                    userInfo.setFdOver(BigDecimal.valueOf(over + winAmount + bettingAmount));
                    userInfoService.updateById(userInfo);
                }
            }
            record.setFdResult(-1);
            record.setFdWinLose(record.getFdBettingAmount());
            record.setFdIsSettlement(1);
            bettingRecordService.updateById(record);
        }
    }
}
